; (function () {
	this.projectConfigUnit = new Vue({
		el: '#unit',
		data: {
			projectId: hlUrlUtil.GetQueryString('projectId'),
			buildingList: [],
			buildingId: null,
			unitColors: [
				{name: '工位单元', class: 'wp', icon: 'icon-Gm-station'},
				{name: '车位单元', class: 'st', icon: 'icon-Gm-parking'},
				{name: '预定', class: 'yd'},
				{name: '已出租', class: 'cz'},
				{name: '即将履约', class: 'ly'},
				{name: '即将到期', class: 'dq'},
				{name: '已到期', class: 'fn'},
				{name: '自用', class: 'zy'},
				{name: '空置', class: 'kz'},
			],
			buildingFloorUnitList: [],
			unitFloorFlag: false,
			buildingFloorsList: [],
			businessTypeData:[],
			selectedBusinessType:[],
			unitTypeLt: [],
			unitTypeData:[],
			floorId: null,
			floorIndex: 0,
			sort: null,
			increaseNum: null,
			addUnits: [],
			editUnit: { units: [] },
			business: { type: '' , val :'' },
			initArceage: 0,
			deleteAllUnitFlag: false,
			showCheck: false,
			floorNumber: null,
			wpFlag: false,
			wpFloorList: [],
			wpIndex: 0,
			unitWpIndex: 0,
			wpList: [],
			unitId: null,
			wps: [],
			addWps: [],
			wpcheckallFlag: false,
			wpNames: [],
			stFlag: false,
			stInfoFlag: false,
			positionType: 'WP',
			floorName: '',
			unitName: '',
			floorWPCount: 0,
			floorSTCount: 0,
			buildingSTCount: 0,
			buildingWPCount: 0,
			isShowSplitUnit:false,
			isShowDeleteUnitBtn:true,
			isShowSplitUnitBtn:true,
			isShowAddUnitBtn:true,
			isShowSplitUnitRadio:false,
			addSplitUnit:[],
			splitUnitNum:null,
			splitUnitIndex:null,
			splitChildUnitIndex:null,
			splitParentUnitAcreage:0,
			quanxian:JSON.parse(localStorage.getItem('permissions')),
		},
		filters: {
			toFixed2 (str) {
				return hlNumberUtil.formatNum(str)
			}
		},
		computed: {
			floorArceage () {
				let result = 0;
				this.editUnit.units.map(item => {
					result += Number(item.acreage)
				})
				return result
			},
			unitTotal () {
				let result = 0;
				this.buildingFloorUnitList.map(item => {
					result += item.unitsLen
				})
				return result
			}
		},
		methods: {
			//获取楼宇
			buildingData () {
				hlhttp.get({
					url: 'project/buildings',
					data: {project_id: this.projectId},
					success: (data) => {
						if (data.length) {
							this.buildingId = data[0].buildingId;
							this.bfUnit()
						}
						this.buildingList = data;
					}
				})
			},
			//选择楼宇
			selBuilding (data) {
				this.buildingId = data.buildingId;
				this.bfUnit()
			},
			//获取某一楼宇下面所有楼层单元
			bfUnit () {
				hlhttp.get({
					url: 'building/units',
					data: {project_id: this.projectId, building_id: this.buildingId},
					success: (data) => {
						data.map(item => {
							item.acreage = 0;
							item.unitsLen = item.units.length;
							if (item.units.length) {
								item.units.map(item0 => {
									item.acreage += Number(item0.acreage)
								})
							}
						})
						this.buildingFloorUnitList = data;
					}
				})
			},
			//显示单元配置
			showUnitFloorConfig () {
				this.unitFloorFlag = true;
				this.isShowSplitUnit = false
				this.addSplitUnit = []
				this.isShowSplitUnitRadio = false
				this.isShowAddUnitBtn = true
				this.isShowDeleteUnitBtn = true
				this.isShowSplitUnitBtn = true
				this.showCheck = false


				this.buildingFloors()
				this.floorTypeDt()
				this.unitType()
			},
			//获取当前楼宇下面楼层
			buildingFloors () {
				hlhttp.get({
					url: 'building/floors',
					data: {project_id: this.projectId, building_id: this.buildingId},
					success: (data) => {
						if (this.floorNumber || this.floorNumber == 0) {
							data.map((item, index) => {
								if (this.floorId == item.floorId) {
									this.floorIndex = index
								}
							})
						}
						if (data.length) {
							this.floorId = data[this.floorIndex].floorId;
							this.$refs.buildingFloor.innerHTML = data[this.floorIndex].floorName;
							this.floorUnitsDt()
						}
						this.buildingFloorsList = data;
					}
				})
			},
			//获取下层
			downFloors () {
				this.floorNumber = null;
				if (this.addUnits.length) {
					hl.toast('请先保存当前单元')
					return
				}
				if (this.floorIndex < this.buildingFloorsList.length-1) {
					this.floorIndex++;
					this.buildingFloors()
				}
			},
			//获取上层
			upFloors () {
				this.floorNumber = null;
				if (this.addUnits.length) {
					hl.toast('请先保存当前单元')
					return
				}
				if (this.floorIndex > 0) {
					this.floorIndex--;
					this.buildingFloors()
				}
			},
			//选择楼层
			selectFloors (data, i) {
				this.floorIndex = i;
				this.buildingFloors()
			},
			//获取楼层业态
			floorTypeDt () {
				hlhttp.get({
					url: 'unit/business_types',
					success: (data) => {
						this.floorTypeList = data;
						this.businessTypeData = hlArrayUtil.replaceData(data, [{key: 'memo', rKey: 'name'}]);
					}
				})
			},
			// 获取单元业态
			unitType () {
				hlhttp.get({
					url: 'unit_types',
					success: (data) => {
						this.unitTypeLt = data;
						this.unitTypeData = hlArrayUtil.replaceData(data, [{key: 'memo', rKey: 'name'}]);
					}
				})
			},
			//获取楼层下面所有单元
			floorUnitsDt () {
				hlhttp.get({
					url: 'floor/units,acerage',
					data: {project_id: this.projectId, building_id: this.buildingId, floor_id: this.floorId},
					success: (data) => {
						this.initArceage = data.floorAcreage;
						data.units.map(item => {
							item.isDeletedUnit = 'N'
							item.isSelectedSplit = 'N'
							item.unitType = item.unitType ? item.unitType.split() : []
							item.unitTypeVal = item.unitTypeVal ? item.unitTypeVal.split() : []
							item.businessType = item.businessType ? item.businessType.split() : []
							item.businessTypeVal = item.businessTypeVal ? item.businessTypeVal.split() : []
							if(item.rentState == 'VA' || item.rentState == 'ZY'){
								item.isDisabled = 'N'
							}else{
								item.isDisabled = 'Y'
							}
							// 给分割的子单元添加删除标识符
							if(item.childUnits){
								item.isDisabled = 'Y'
								item.childUnits.forEach(el => {
									el['isDeletedSplitUnit'] = 'N'
									el.unitType = el.unitType ? el.unitType.split() : []
									el.unitTypeVal = el.unitTypeVal ? el.unitTypeVal.split() : []
									el.businessType = el.businessType ? el.businessType.split() : []
									el.businessTypeVal = el.businessTypeVal ? el.businessTypeVal.split() : []
									el.isDisabled = el.rentState == 'VA' || el.rentState == 'ZY' ? 'N' : 'Y'
								})
							}else{
								item.childUnits = []
							}

						})

						this.editUnit = data

						console.log(	this.editUnit );
					}
				})
			},
			//添加一条楼宇
			addUnitsDtph () {
				if (this.addUnits.length) {
					hl.toast('请先保存后在添加')
					return false
				}

				this.addUnits.push({
					businessType: '',
					businessTypeVal: '',
					unitName: '',
					acreage: '',
					unitType: '',
					unitTypeVal: '',
					myself: 'N',
					isDeletedUnit: 'N',
					isSelectedSplit: 'N',
					splitFlag:'N',
					childUnits:[],
				})
			},
			//删除某条单元
			delUnit (i) {
				this.addUnits.splice(i, 1)
			},
			// 选择新增业态
			selectAddBusinessType(i,data){
				this.addUnits[i].businessType = data[0].code.split();
				this.addUnits[i].businessTypeVal = data[0].name.split();
			},
			// 选择新增单元类型
			selectAddUnitType(i,data){
				this.addUnits[i].unitType = data[0].code.split();
				this.addUnits[i].unitTypeVal = data[0].name.split();
			},
			// 选择编辑业态
			selectEditBusinessType(i,data){
				this.editUnit.units[i].businessType = data[0].code.split();
				this.editUnit.units[i].businessTypeVal = data[0].memo.split();
			},
			// 选择编辑类型
			selectEditUnitType(i,data){
				this.editUnit.units[i].unitType = data[0].code.split();
				this.editUnit.units[i].unitTypeVal = data[0].memo.split();
			},
			selectBusinessType(data){
				this.business = {type:data[0].code.split(), val:data[0].name.split()}
			},
			// 选择编辑分割单元业态
			selectEditSplitUnitBusinessType(unitIndex,splitUnitIndex,data){
				const splitUnits = this.editUnit.units[unitIndex].childUnits[splitUnitIndex]

				splitUnits.businessType =  data[0].code.split();
				splitUnits.businessTypeVal =  data[0].memo.split();
			},
			// 选择编辑分割单元类型
			selectEditSplitUnitType(unitIndex,splitUnitIndex,data){
				const splitUnits = this.editUnit.units[unitIndex].childUnits[splitUnitIndex]

				splitUnits.unitType =  data[0].code.split();
				splitUnits.unitTypeVal =  data[0].memo.split();
			},
			// 选择分割单元
			selectSplitUnit(i){
				this.splitUnitIndex = i
				this.splitParentUnitAcreage = this.editUnit.units[i]['acreage']
				this.editUnit.units.forEach((el,index) => {
					if(i == index) el['isSelectedSplit'] = 'Y'
					if(i != index) el['isSelectedSplit'] = 'N'
				})

			},
			// 确认分割单元
			confirmSplitUnit(){
				if(this.splitUnitIndex == null){
					hl.toast('请先选择要分割的单元')
					return false
				}

				const units = this.editUnit.units
				const num = this.splitUnitNum , originNum = units[this.splitUnitIndex]['childUnits'] ? units[this.splitUnitIndex]['childUnits'].length : 0
				const projectId = units[0]['projectId'] , buildingId = units[0]['buildingId'] , floorId = units[0]['floorId']
				const floor = units[0]['floor']

				// 分割单元总数量加起来不超过5个
				const totalNum = num + originNum

				if(totalNum > 5){
					hl.toast('分割单元数量至多5个')
					return false
				}
				if(totalNum < 2){
					hl.toast('分割单元数量至少2个')
					return false
				}

				// addSplitUnit 用来存放新增的分割单元，当分割单元确认后，合并到 editUnit 中
				let addSplitUnit = []
				let tmpUnitObj = {
					'unitName':null,
					'unitId':null,
					'unitType':[],
					'unitTypeVal':[],
					'floor':floor,
					'floorId':floorId,
					'buildingId':buildingId,
					'businessType':[],
					'businessTypeVal':[],
					'locateType':'N',
					'splitFlag':'N',
					'isDeletedSplitUnit':'N',
					'acreage':null,
					'myself':'N',
					'projectId':projectId,
					'rentState':'VA',
					'isError':'N'
				}

				for (var i = 0; i < num; i++) {
					addSplitUnit.push(JSON.parse(JSON.stringify(tmpUnitObj)))
				}

				this.addSplitUnit = addSplitUnit
				this.isShowSplitUnitRadio = false
				this.isShowSplitUnit = false
				this.isShowAddUnitBtn = true
				this.isShowDeleteUnitBtn = true
				this.isShowSplitUnitBtn = true
				this.showCheck = false
				this.splitUnitNum = null
			},
			// 选择新增的分割单元的业态类型
			selectAddSplitBusinessType(i,data){
				this.addSplitUnit[i].businessType = data[0].code.split();
				this.addSplitUnit[i].businessTypeVal = data[0].memo.split();
			},
			// 选择新增的分割单元的单元类型
			selectAddSplitUnitType(i,data){
				this.addSplitUnit[i].unitType = data[0].code.split();
				this.addSplitUnit[i].unitTypeVal = data[0].memo.split();
			},
			// 是否自用---新增分割单元
			addSplitIsMyself(i) {
				this.addSplitUnit[i].myself = this.addSplitUnit[i].myself == 'N' ? 'Y' : 'N'
			},
			// 将分割单元转换为自用
			changeSplitUnitToMyself(unitIndex,splitUnitIndex,item){
				if(item.isDisabled == 'Y') return
				const splitUnits = this.editUnit.units[unitIndex].childUnits[splitUnitIndex]

				splitUnits.myself = splitUnits.myself == 'Y' ? 'N' : 'Y'
			},
			validateSplitUnit(unit){
				if(!unit.businessType.length){
					hl.toast('分割业态类型不能为空')
					return false
				}
				if(!unit.unitName){
					hl.toast('分割单元名称不能为空')
					return false
				}
				if(!unit.acreage){
					hl.toast('分割单元面积不能为空')
					return false
				}
				if(!unit.unitType.length){
					hl.toast('分割单元类型不能为空')
					return false
				}
				// 分割后的子单元总面积不能超过分割单元面积
				const childUnits = this.editUnit.units[this.splitUnitIndex]['childUnits']
				const parentUnitAcreage = Number(this.splitParentUnitAcreage)
				let addSplitUnitAcreage = Number(unit.acreage)
				let otherSplitUnitAcreage = 0

				childUnits.forEach(el => otherSplitUnitAcreage += Number(el['acreage']) )

				if( Number(otherSplitUnitAcreage + addSplitUnitAcreage).toFixed(2) > parentUnitAcreage && childUnits.length !== 0 ){
					this.addSplitUnit[this.splitChildUnitIndex]['isError'] = 'Y'
					hl.toast('子单元总面积与父单元面积不一致')
					return false
				}

				console.log(this.editUnit.units);

				return true
			},
			// 删除新增的分割单元
			delAddSplitUnit(i){
				this.addSplitUnit.splice(i,1)
			},
			// 保存新增的分割单元
			saveAddSplitUnit(i){
				this.splitChildUnitIndex = i

				const splitUnitIndex = this.splitUnitIndex , units = this.editUnit.units
				const addSplitUnitArr = new Array()
				const valid = this.validateSplitUnit(this.addSplitUnit[i])

				if(!valid) return false

				addSplitUnitArr.push(this.addSplitUnit[i])

				units[splitUnitIndex].childUnits = units[splitUnitIndex].childUnits.concat(addSplitUnitArr)
				units[splitUnitIndex].splitFlag = 'Y'

				this.addSplitUnit.splice(i,1)

			},
			// 删除分割的单元
			deleteSplitUnit(unitIndex,splitUnitIndex){
				const splitUnits = this.editUnit.units[unitIndex].childUnits[splitUnitIndex]

				if (splitUnits.rentState == 'VA' || splitUnits.rentState == 'ZY' || splitUnits.unitId == null ) {
					if(splitUnits.isDeletedSplitUnit == 'Y'){
						splitUnits.isDeletedSplitUnit = 'N'
					}else{
						splitUnits.isDeletedSplitUnit = 'Y'
					}

				} else {
					hl.toast('不能选择已经出租的单元')
				}

			},
			// 取消分割单元
			cancelSplitUnit(){
				if(this.addSplitUnit.length){
					hl.toast('请先保存分割操作')
					return false
				}

				this.isShowSplitUnit = false
				this.isShowAddUnitBtn = true
				this.isShowSplitUnitBtn = true
				this.showCheck = false
				this.isShowDeleteUnitBtn = true
				this.isShowSplitUnitRadio = false
			},
			splitUnit(){
				this.isShowSplitUnit = true
				this.isShowAddUnitBtn = false
				this.isShowSplitUnitBtn = false
				this.showCheck = false
				this.isShowDeleteUnitBtn = false
				this.isShowSplitUnitRadio = true
			},
			//保存单条单元
			saveUnit (i) {
				if (!this.addUnits[i].businessType) {
					hl.toast('请选择业态')
					return
				}
				if (!this.addUnits[i].unitName) {
					hl.toast('请输入单元名')
					return
				}
				if (!this.addUnits[i].acreage) {
					hl.toast('请输入面积')
					return
				}
				if (!this.addUnits[i].unitType) {
					hl.toast('请选择类型')
					return
				}

				this.editUnit.units = this.editUnit.units.concat(this.addUnits[i])
				this.addUnits.splice(i, 1)
			},
			//验证单个单元名称
			validateUnit (i) {
				let flag = false;
				this.editUnit.units.map(item => {
					if (item.unitName == this.addUnits[i].unitName) {
						flag = true
					}
				})
				if (flag) {
					hl.toast('单元名称不可重复')
					this.addUnits[i].unitName = ''
				}
			},
			//是否自用---新增
			myselfIs (i) {
				this.addUnits[i].myself = this.addUnits[i].myself == 'N' ? 'Y' : 'N'
			},
			//是否自用---编辑
			myselfIsedit (i,item) {
				if(item.isDisabled == 'Y') return 
				this.editUnit.units[i].myself = this.editUnit.units[i].myself == 'N' ? 'Y' : 'N'
			},
			//选择业态
			selBusinnessType (data) {
				this.business = {type: data.code, val: data.memo}
			},
			//生成多个单元
			addMoreUnit () {
				if (this.addUnits.length) {
					hl.toast('请先保存当前单元')
					return
				}
				if (!this.business.type) {
					hl.toast('请选择业态')
					return
				}

				let unitName = lastNumber(this.sort)[0];
				let unitSort = lastNumber(this.sort)[1];
				let initNum = lastNumber(this.sort)[1];
				let arr = JSON.parse(JSON.stringify(this.editUnit.units));

				if (this.sort && this.increaseNum) {
					for (let i = 0; i < this.increaseNum; i++) {
						arr.push({
							businessType: this.business.type,
							businessTypeVal: this.business.val,
							unitName: unitName + lackMorals(unitSort, initNum),
							acreage: '0',
							unitType: this.unitTypeData[0].code.split(),
							unitTypeVal: this.unitTypeData[0].name.split(),
							myself: 'N',
							splitFlag: 'N',
							isDeletedUnit: 'N',
							isSelectedSplit: 'N',
							childUnits:[]
						})

						unitSort++
					}
					arr = hlArrayUtil.removeRepeat(arr, 'unitName');
					this.editUnit.units = JSON.parse(JSON.stringify(arr))
				}
			},
			//清除新增信息
			addEditInfo () {
				this.unitFloorFlag = false;
				this.sort = null;
				this.increaseNum = null;
				this.$refs.buildingFloor.innerHTML = '请选择';
				this.editUnit.units = [];
				this.addUnits = [];
			},
			validateAddUnit(){
				if(this.addSplitUnit.length){
					hl.toast('请先保存分割操作')
					return false
				}
				if(this.addUnits.length){
					hl.toast('请先保存当前单元')
					return false
				}
				if(!this.floorId) {
					hl.toast('请先选择楼层')
					return false
				}

				const units = this.editUnit.units
				for (var i = 0; i < units.length; i++) {
					if(units[i].childUnits){
						if(units[i].childUnits.length == 1){
							hl.toast('请删除全部分割单元，或保留两个以上')
							return false
						}
					}
				}

				return true
			},
			// 确认添加
			queryAddUnit () {

				let data = [] , valid = this.validateAddUnit()

				if(!valid) return true

				this.editUnit.units.map(item => {
					let childUnits = []

					if(item.childUnits){
						item.childUnits.forEach(el => {
							childUnits.push({
								'id':el.unitId || '#',
								'name':el.unitName ,
								'acreage': el.acreage ,
								'myself': el.myself ,
								'business_type': el.businessType.join() ,
								'unit_type': el.unitType.join()
							})
						})
					}

					data.push({
						id: item.unitId || '#',
						name: item.unitName,
						acreage: item.acreage,
						business_type: item.businessType.join(),
						unit_type: item.unitType.join(),
						myself: item.myself,
						child_units:childUnits.length ? JSON.stringify(childUnits) : '#'
					})
				})

				hlhttp.post({
					url: 'floor/units',
					data: {
						project_id: this.projectId,
						building_id: this.buildingId,
						floor_id: this.floorId,
						floor_name: this.$refs.buildingFloor.innerHTML,
						units: JSON.stringify(data)
					},
					success: (data) => {
						hl.toast('新增成功')
						this.addEditInfo()
						this.bfUnit()
						this.closeUnitConfig()
					}
				})
			},
			//关闭单元配置
			closeUnitConfig () {
				this.floorIndex = 0;
				this.unitFloorFlag = false;
				this.floorNumber = null;
				this.addUnits = [];
				this.sort = null;
				this.increaseNum = null;
			},
			// 全选删除功能
			deleteAllUnit() {
				const units = this.editUnit.units
				this.deleteAllUnitFlag = !this.deleteAllUnitFlag;

				// 正选
				const positiveSelectAll = () => {
					units.forEach(el => {
						if (el.rentState == 'VA' || el.rentState == 'ZY' || !el.unitId) el.isDeletedUnit = 'Y'

						el.childUnits.forEach(el1 => {
							if (el1.rentState == 'VA' || el1.rentState == 'ZY' || !el1.unitId) el1.isDeletedSplitUnit = 'Y'
						})
					})
				}
				// 反选
				const inverseSelectAll = () => {
					units.forEach(el => {
						if (el.rentState == 'VA' || el.rentState == 'ZY' || !el.unitId) el.isDeletedUnit = 'N'

						el.childUnits.forEach(el1 => {
							if (el1.rentState == 'VA' || el1.rentState == 'ZY' || !el1.unitId) el1.isDeletedSplitUnit = 'N'
						})
					})
				}

				if (this.deleteAllUnitFlag){
					positiveSelectAll()
				}else{
					inverseSelectAll()
				}

			},
			// 删除单元
			deleteUnit (i) {
				const units = this.editUnit.units

				if (units[i].rentState == 'VA' || units[i].rentState == 'ZY' || !units[i].unitId) {
					this.deleteAllUnitFlag = false;

					if( units[i].isDeletedUnit == 'Y' ){
						units[i].isDeletedUnit = 'N'
					}else{
						units[i].isDeletedUnit = 'Y'
					}

				} else {
					hl.toast('不能选择已经出租的单元')
				}
			},
			//显示删除选择按钮
			showDelButton () {
				this.showCheck = !this.showCheck;
				this.wpNames = [];
				this.wpcheckallFlag = false;
			},
			// 删除验证
			validateDeleteUnit(){
				// 存在分割的子单元时，必须删除掉全部子单元或至少留两个以上的子单元
				const units = this.editUnit.units , valid = true

				units.forEach((el,index) => {
					if(valid){
						let count = 0,splitUnitsNum = el.childUnits ? el.childUnits.length : 0

						if(el.childUnits){
							el.childUnits.forEach((el1,index1) => {
								if(el1.isDeletedSplitUnit == 'Y') count++
							})
						}

						if( splitUnitsNum - count == 1 ){
							hl.toast('请删除全部分割单元，或保留两个以上')
							valid = false
						}
					}
				})

				return valid

			},
			// 确认删除
			delMoreUnits () {
				let unitIds = [] , units = this.editUnit.units , self = this
				let valid = this.validateDeleteUnit()

				if(valid == false) return false

				units.forEach((el,index) => {
					if(el.isDeletedUnit == 'Y' && el.unitId) unitIds.push(el.unitId)
					if(el.childUnits){
						el.childUnits.forEach((el1,index1) => {
							if(el1.isDeletedSplitUnit == 'Y' && el1.unitId) unitIds.push(el1.unitId)
						})
					}
				})

				hllayer.confirm('你确认要删除选中的单元吗?（子单元删除后，该单元的所有合同、意向书、账单将合并至父单元）', () => {
					if(unitIds.length == 0){
						self.floorUnitsDt()
						return false
					}

					hlhttp.delete({
						url: 'unit?unit_id=' + unitIds.toString() ,
						success: () => {
							self.floorUnitsDt()
						}
					})
				})

			},
			//编辑
			editFloorUnit (data) {
				this.unitFloorFlag = true;
				this.floorNumber = data.floor;
				this.floorId = data.floorId;

				this.unitFloorFlag = true;
				this.isShowSplitUnit = false
				this.addSplitUnit = []
				this.isShowSplitUnitRadio = false
				this.isShowAddUnitBtn = true
				this.isShowDeleteUnitBtn = true
				this.isShowSplitUnitBtn = true
				this.showCheck = false

				this.buildingFloors()
				this.floorTypeDt()
				this.unitType()
			},
			//工位配置
			wpConfig () {
				this.positionType = 'WP';
				this.unitWpIndex = 0;
				this.wpFlag = true;
				this.floorUnitsWp()
			},
			//获取工位楼层单元
			floorUnitsWp () {
				hlhttp.get({
					url: 'buildings/st_units,wp_units',
					data: {opt: 'WP', building_id: this.buildingId},
					success: (data) => {
						if (!data.length) return;
						if (this.floorId) {
							data.map((item, index) => {
								if (item.floorId == this.floorId) {
									this.floorIndex = index;
								}
							})
						}
						if (this.unitId) {
							data.map((item, index) => {
								item.units.map((item0, index0) => {
									if (item0.unitId == this.unitId) {
										this.unitWpIndex = index0
									}
								})
							})
						}
						if (data.length) {
							this.$refs.stwpFloors.innerHTML = data[this.floorIndex].floorName;
							this.floorId = data[this.floorIndex].floorId;
							this.buildingwpstNumber()
							this.floorstwpNumber()
							if (data[this.floorIndex].units.length) {
								this.$refs.stwpUnits.innerHTML = data[this.floorIndex].units[this.unitWpIndex].unitName;
								this.unitId = data[this.floorIndex].units[this.unitWpIndex].unitId;
								this.wpDt()
							}
						}
						this.wpFloorList = data;
						this.wpList = data[this.wpIndex].units
					}
				})
			},
			//获取工位
			wpDt () {
				hlhttp.get({
					url: 'units/positions',
					data: {unit_id: this.unitId},
					success: (data) => {
						this.wps = data;
						this.wps.map(item=>{
							if(item.rentState == 'VA' || item.rentState == 'ZY'){
								this.wps.push(item.isDisabled = 'N')
							}else{
								this.wps.push(item.isDisabled = 'Y')
							}
							
						})
					}
				})
			},
			//添加单个工位
			addWp () {
				if (this.addWps.length) {
					hl.toast('请先保存工位')
					return
				}
				this.addWps.push({positionName: '', positionType: this.positionType, rentType: 'RE'})
			},
			//添加多个工位
			addMoreWp () {
				if (!this.sort && !this.increaseNum) return;
				let data = lastNumber(this.sort);
				let name = data[0];
				let num = data[1];
				let num1 = data[1];
				for (var i = 0; i < this.increaseNum; i++) {
					this.wps.push({positionName: name + lackMorals(num, num1), positionType: this.positionType, rentType: 'RE'})
					num++
				}
				this.wps = hlArrayUtil.removeRepeat(this.wps, 'positionName')
			},
			// 自选
			addMyself (i,item) {
				if(item.isDisabled == 'Y') return false
				this.addWps[i].rentType = this.addWps[i].rentType == 'ZY' ? 'RE' : 'ZY'
			},
			//确认
			addWpQuery (i) {
				this.wps.push(this.addWps[i])
				this.addDelWp(i)
			},
			//删除
			addDelWp (i) {
				this.addWps.splice(i, 1)
			},
			//编辑
			editMyself (i,item) {
				if(item.isDisabled == 'Y') return
				this.wps[i].rentType = this.wps[i].rentType == 'ZY' ? 'RE' : 'ZY'
			},
			//确认添加工位
			queryWp () {
				if (!this.floorId) {
					hl.toast('请先选择楼层')
					return
				}
				if (this.addWps.length) {
					hl.toast('请先保存当前工位')
					return
				}

				let data = [];
				this.wps.map(item => {
					data.push({
						"id": item.positionId || '#',
						"name": item.positionName,
						"unit_type": item.positionType,
						"rent_type": item.rentType
					})
				})

				hlhttp.post({
					url: 'units/positions',
					data: {
						project_id: this.projectId,
						floor_id: this.floorId,
						building_id: this.buildingId,
						unit_id: this.unitId,
						positions: JSON.stringify(data)
					},
					success: () => {
						this.wpDt()
						this.closeWp()
					}
				})
			},
			//关闭工位配置
			closeWp () {
				this.wpFlag = false;
				this.sort = null;
				this.increaseNum = null;
				this.unitId = null;
				this.floorId = null;
				this.unitWpIndex = 0;
				this.stFlag = false;
			},
			//选择工位楼层
			selectWpFloor (data) {
				this.unitWpIndex = 0;
				this.floorId = data.floorId;
				this.wpList = data.units;
				this.$refs.stwpUnits.innerHTML = this.wpList[0].unitName;
				this.unitId = this.wpList[0].unitId;
				this.wpDt()
			},
			//选择单元
			selectUnit (data, i) {
				this.unitId = data.unitId;
				this.unitWpIndex = i;
				this.wpDt()
			},
			//工位全选
			wpCheckall () {
				this.wpcheckallFlag = !this.wpcheckallFlag
				if (this.wpcheckallFlag) {
					this.wps.map(item => {
						this.wpNames.push(item.positionName)
					})
				} else {
					this.wpNames = []
				}
			},
			//单选工位
			checkWp (i) {
				this.wpcheckallFlag = false;
				if (this.wpNames.indexOf(this.wps[i].positionName) > -1) {
					this.wpNames.splice(this.wpNames.indexOf(this.wps[i].positionName), 1)
				} else {
					this.wpNames.push(this.wps[i].positionName)
				}
			},
			//确认删除工位
			delWps () {
				let idsArr = [];
				let newArr = [];
				let newFlag = false;
				this.wps.map(item => {
					let flag = false;
					this.wpNames.map(item0 => {
						if (item.positionName == item0) {
							if (item.positionId) {
								idsArr.push(item.positionId)
							}
							flag = true;
							newFlag = true;
						}
					})
					if (!flag) {
						newArr.push(item)
					}
				})

				hllayer.confirm('你确认要删除选中的单元吗?', () => {
					if (newFlag && !idsArr.length) {
						this.wps = JSON.parse(JSON.stringify(newArr))
						this.showCheck = false;
						this.wpNames = [];
						return
					}
					hlhttp.delete({
						url: 'units/positions?position_ids='+idsArr.toString(),
						success: () => {
							this.wps = JSON.parse(JSON.stringify(newArr))
							this.showCheck = false;
							this.wpNames = [];
						}
					})
				})
			},
			// 上一层单元
			upUnits () {
				if (this.unitWpIndex > 0) {
					this.unitWpIndex--;
					this.unitId = this.wpList[this.unitWpIndex].unitId;
					this.$refs.stwpUnits.innerHTML = this.wpList[this.unitWpIndex].unitName;
					this.wpDt()
				}
			},
			//下一层单元
			downUnits () {
				if (this.unitWpIndex < this.wpList.length-1) {
					this.unitWpIndex++;
					this.unitId = this.wpList[this.unitWpIndex].unitId;
					this.$refs.stwpUnits.innerHTML = this.wpList[this.unitWpIndex].unitName;
					this.wpDt()
				}
			},
			//车位配置
			stConfig () {
				this.positionType = 'ST';
				this.unitWpIndex = 0;
				this.stFlag = true;
				this.floorUnitsSt()
			},
			//获取工位楼层单元
			floorUnitsSt () {
				hlhttp.get({
					url: 'buildings/st_units,wp_units',
					data: {opt: 'ST', building_id: this.buildingId},
					success: (data) => {
						if (!data.length) return;
						if (this.floorId) {
							data.map((item, index) => {
								if (item.floorId == this.floorId) {
									this.floorIndex = index;
								}
							})
						}
						if (this.unitId) {
							data.map((item, index) => {
								item.units.map((item0, index0) => {
									if (item0.unitId == this.unitId) {
										this.unitWpIndex = index0
									}
								})
							})
						}
						if (data.length) {
							this.$refs.stwpFloors.innerHTML = data[this.floorIndex].floorName;
							this.floorId = data[this.floorIndex].floorId;
							this.buildingwpstNumber()
							this.floorstwpNumber()
							if (data[this.floorIndex].units.length) {
								this.$refs.stwpUnits.innerHTML = data[this.floorIndex].units[this.unitWpIndex].unitName;
								this.unitId = data[this.floorIndex].units[this.unitWpIndex].unitId;
								this.wpDt()
							}
						}
						this.wpFloorList = data;
						this.wpList = data[this.wpIndex].units
					}
				})
			},
			//获取单元下工位数
			wpStation (data, data0) {
				if (!(data0.unitType == 'ST') && !(data0.unitType == 'WP')) return;
				this.stInfoFlag = true;
				this.unitId = data0.unitId;
				this.floorId = data.floorId;
				this.positionType = data0.unitType;
				this.wpDt()
				this.buildingwpstNumber()
				this.floorstwpNumber()
			},
			//关闭车位详情
			stwpClose () {
				this.stInfoFlag = false;
			},
			//编辑车位工位
			editStWpKoil () {
				this.stInfoFlag = false;
				this.stFlag = true;
				if (this.positionType == 'ST') {
					this.floorUnitsSt()
				}
				if (this.positionType == 'WP') {
					this.floorUnitsWp()
				}
			},
			//获取楼宇工位车位数
			buildingwpstNumber () {
				hlhttp.get({
					url: 'project/building_position_count',
					data: {building_id: this.buildingId},
					success: (data) => {
						this.buildingWPCount = data.buildingWPCount;
						this.buildingSTCount = data.buildingSTCount;
					}
				})
			},
			//获取耧层车位工位数
			floorstwpNumber () {
				hlhttp.get({
					url: 'project/floor_position_count',
					data: {floor_id: this.floorId},
					success: (data) => {
						this.floorWPCount = data.floorWPCount;
						this.floorWPCount.map(item=>{
							if(item.rentState == 'VA' || item.rentState == 'ZY'){
								this.wps.push(item.isDisabled = 'N')
							}else{
								this.wps.push(item.isDisabled = 'Y')
							}
							
						})
						this.floorSTCount = data.floorSTCount;
						this.floorSTCount.map(item=>{
							if(item.rentState == 'VA' || item.rentState == 'ZY'){
								this.wps.push(item.isDisabled = 'N')
							}else{
								this.wps.push(item.isDisabled = 'Y')
							}
							
						})
					}
				})
			},
			submitProjectConfig(){
				window.projectConfigTab.active = '平面图配置';
				window.projectConfigTab.menuShow()
			}
		},
		created () {
			hlSelectUi()
		},
		mounted () {
			this.buildingData()
		}
	})

	function lastNumber (str) {
		var match = str.match(/(\d+$)/g);
		var m0 = '';
		if (!isNaN(str)) {
			return [ '', str]
		}
		if (!match) {
			return [ str, 0]
		}
		return [str.substr(0, str.length-match[0].length), match[0]]
	}

	function lackMorals (num, initNum) {
		var len = initNum.split('').length;
		var number = parseInt(num, 10);
		number = number.toString()
		while(number.length < len) {
		    number = '0' + number;
		}
		return number
	}
} ())
